﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TraineeManagement.Models.BusinessObject;
using TraineeManagement.Models;
using System.IO;

namespace TraineeManagement.Controllers
{
    public class TraineeController : Controller
    {

        private TraineeBO TBO = new TraineeBO();

        public ActionResult All()
        {
            try
            {
                List<Trainee> Trainees = TBO.GetAllTrainees();
                return View(Trainees);
            }
            catch (Exception)
            {
                return RedirectToAction("Index", "Error");
            }
        }

        public ActionResult New()
        {
            try
            {
                List<Trainee> Trainees = TBO.GetNewTrainees();
                return View(Trainees);
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

        public ActionResult Expiring()
        {
            try
            {
                List<Trainee> Trainees = TBO.GetExpiringTrainees();
                return View(Trainees);
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

        public ActionResult TraineeInfo(int id)
        {
            try
            {
                SessionBO SessionBO = new SessionBO();
                Trainee Trainee = TBO.GetTraineeById(id);
                ViewBag.Sessions = SessionBO.GetAllSessionsOfTrainee(id).OrderBy(s => s.StartDate).ToList();
                return View(Trainee);
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

        [HttpPost]
        public ActionResult AddTrainee(Trainee trainee, Session session, HttpPostedFileBase file)
        {
            try
            {
                if (trainee.FullName != null && !trainee.FullName.Equals(""))
                {
                    trainee.PhotoUrl = "";
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/Content/TraineePhoto/"), fileName);
                        file.SaveAs(path);
                        trainee.PhotoUrl = fileName;
                    }

                    if (!(session.StartDate == DateTime.MinValue) && !(session.EndDate == DateTime.MinValue))
                    {
                        trainee.Sessions.Add(session);
                    }
                    TBO.AddTrainee(trainee);

                }
                return RedirectToAction("All");
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

        [HttpPost]
        public ActionResult Update(Trainee trainee, HttpPostedFileBase file)
        {
            try
            {
                Trainee OriTrainee = TBO.GetTraineeById(trainee.TraineeID);

                trainee.PhotoUrl = "";
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Content/TraineePhoto/"), fileName);
                    file.SaveAs(path);
                    OriTrainee.PhotoUrl = fileName;
                }
                if (trainee.FullName != null && !trainee.FullName.Equals(OriTrainee.FullName))
                {
                    OriTrainee.FullName = trainee.FullName;
                }

                TBO.UpdateTrainee(OriTrainee);
                return RedirectToAction("TraineeInfo/" + trainee.TraineeID);
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                Trainee Trainee = TBO.GetTraineeById(id);
                TBO.DeleteTrainee(Trainee);
                return RedirectToAction("All");
            }
            catch (Exception)
            {

                return RedirectToAction("Index", "Error");
            }
        }

    }
}
